package it.amicosmanettone.whatsimage;

import java.util.List;
import java.util.Locale;

import it.amicosmanettone.whatsimage.adapter.ImagesListAdapter;
import it.amicosmanettone.whatsimage.client.ApiClient;
import it.amicosmanettone.whatsimage.jsonbean.Image;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;

public class ImagesActivity extends Activity {
	
	static ListView listCategories = null;
	static ImagesListAdapter adapter = null;
	public static List<Image> listImages = null;
	
	String categoryLabel = null;
	String ORDER = "NORMAL";
	String LANGUAGE = "All";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_images);
		
		Spinner order = (Spinner)findViewById(R.id.images_spinner_order);
		String orderNormal = getText(R.string.images_order_normal).toString();
		String orderRanking = getText(R.string.images_order_ranking).toString();
		String orderDate = getText(R.string.images_order_date).toString();
		String orderRandom = getText(R.string.images_order_random).toString();
		
        ArrayAdapter<String> spinnerOrderAdapter = new ArrayAdapter<String>(
        		this,
        		android.R.layout.simple_spinner_item,
        		new String[]{orderNormal,orderRanking,orderDate,orderRandom}
        		);
        order.setAdapter(spinnerOrderAdapter);
        
        Spinner language = (Spinner)findViewById(R.id.images_spinner_language);
        
        ArrayAdapter<String> spinnerLanguageAdapter = null;
        Log.v("LANGUAGE","Lingua ----->    " + Locale.getDefault().getDisplayLanguage());
        
        if(Locale.getDefault().getDisplayLanguage().equals("English")){
        	
        	spinnerLanguageAdapter = new ArrayAdapter<String>(
            		this,
            		android.R.layout.simple_spinner_item,
            		new String[]{"All","English"}
            		);
        	
        }
        else if(Locale.getDefault().getDisplayLanguage().equals("Italiano")){
        	
        	spinnerLanguageAdapter = new ArrayAdapter<String>(
            		this,
            		android.R.layout.simple_spinner_item,
            		new String[]{"Tutte","Italiano"}
            		);
        	
        }else{
        	
        	spinnerLanguageAdapter = new ArrayAdapter<String>(
            		this,
            		android.R.layout.simple_spinner_item,
            		new String[]{"All","English"}
            		);
        	
        }
        
        
        
        language.setAdapter(spinnerLanguageAdapter);
		
		Bundle extras = getIntent().getExtras();
		
		if (extras != null) {

			this.categoryLabel = extras.getString("categoryLabel");
			this.setTitle(categoryLabel);
			
			listImages = ApiClient.API_GetContentList(categoryLabel, ORDER).getImages();
			
			listCategories = (ListView) findViewById(R.id.images_listImages);

			adapter = new ImagesListAdapter(ImagesActivity.this,
					R.layout.row_images, categoryLabel, ORDER);

			listCategories.setAdapter(adapter);

			adapter.addAll(listImages);

		}else{
			
			Toast.makeText(getApplicationContext(), "ERROR", Toast.LENGTH_LONG).show();
			finish();
			
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		
		getMenuInflater().inflate(R.menu.main, menu);
		/*SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
		String userLogged = sharedPref.getString("userLogged", "GUEST");
		
		if(userLogged.equals("GUEST")){
			menu.removeItem(R.id.action_logout);
		}else{
			menu.removeItem(R.id.action_login);
		}*/
		menu.removeItem(R.id.action_login);
		menu.removeItem(R.id.action_logout);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {

		case android.R.id.home:

			return true;
			
		case R.id.action_info:
			
			Intent informationActivity = new Intent(this, InformationActivity.class);
			startActivity(informationActivity);
			overridePendingTransition(R.xml.translate_in, R.xml.translate_out);

			return true;
			
		case R.id.action_login:
			Intent loginActivity = new Intent(this, LoginActivity.class);
			loginActivity.putExtra("fromActivity", "IMAGES");
			startActivity(loginActivity);
			overridePendingTransition(R.xml.translate_in, R.xml.translate_out);
			
			return true;
		
		case R.id.action_logout:
			Intent logoutActivity = new Intent(this, LoginActivity.class);
			logoutActivity.putExtra("fromActivity", "IMAGES");
			startActivity(logoutActivity);
			overridePendingTransition(R.xml.translate_in, R.xml.translate_out);
			
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

}
